package org.styx.account.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.styx.account.domain.Account;
import org.styx.account.mapper.AccountMapper;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * @date 2021/8/6
 * @auther uranus
 */
@Service
@Slf4j
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 测试
     * @param id
     * @return
     */
    public Account get(Long id){
        return accountMapper.selectById(id);
    }

    /**
     * 扣减账户余额
     * @param userId 用户id
     * @param money 金额
     */
    public void decrease(Long userId, BigDecimal money){
        log.info("------------>account-service中扣减账户余额开始!!!!!!");
        //模拟超时异常，全局事务回滚
        //暂停几秒鈡线程
        try{
            TimeUnit.SECONDS.sleep(30);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
       /* try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        accountMapper.decrease(userId,money);
        log.info("------------>account-service中扣减账户余额结束######");
    }
}
